package cn.lanca.esaypoi.c202204.week_4.tue;

import cn.lanca.esaypoi.c202204.week_3.v4.CommonA;
import cn.lanca.esaypoi.c202204.week_4.sun.CommonABCDEFG;
import cn.lanca.esaypoi.util.tool.CommonAll;
import cn.lanca.esaypoi.util.tool.ExcelUtil;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * project name:<font size = "1"><b>小芄健康数据运营平台</b></font><br>
 * file name:<font size = "1"><b> </b></font><br>
 * description:<font size = "1"><b> </b></font><br>
 * company name:<font size = "1"><b>小芄医药科技（成都）有限公司-研发中心</b></font><br>
 *
 * @author <a href="mail to: meihongliang@xiaowanjiankang.com" rel="nofollow">meihongliang<br>
 * @version 1.0.0<br>
 * @since [1] [2022/4/26 09:56] [meihongliang] [新建] <br>
 */
public class TestTue {
    public static void main(String[] args) throws IOException {

        List<CommonAll> allDBData = ExcelUtil.getAllDBData();
        System.out.println(allDBData.size());
        Map<String, List<CommonAll>> collect = allDBData.stream().collect(Collectors.groupingBy(CommonAll::getA1));
        ExcelUtil.writeSheetsToOneFIle("/Users/meihongliang/Downloads/A_xiaowan/main/数据库全部药品分组.xlsx", collect, CommonAll.class);
//        System.out.println(collect);
//        ExcelUtil.writeExcel(allDBData, CommonAll.class, "/Users/meihongliang/Downloads/A_xiaowan/main/20220426数据库全部数据_" + allDBData.size() + ".xlsx");

    }

    private static void extracted() throws IOException {
        List<CommonA> commonA = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/20220426_0_女性常用OTC匹配上de数据库中数据.xlsx", CommonA.class);
        List<CommonA> commonB = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/20220426_1_肛肠常用OTC匹配上de数据库中数据.xlsx", CommonA.class);
        List<CommonA> commonC = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/20220426_2_儿童常用OTC匹配上de数据库中数据.xlsx", CommonA.class);
        List<CommonA> commonD = ExcelUtil.readExcel("/Users/meihongliang/Downloads/A_xiaowan/download/20220426_3_女性常用其他匹配上de数据库中数据.xlsx", CommonA.class);
        System.out.println(commonA.size());
        System.out.println(commonB.size());
        System.out.println(commonC.size());
        System.out.println(commonD.size());

        for (int i = 0; i < commonA.size(); i++) {
            CommonA commonA1 = commonA.get(i);
            String a18 = commonA1.getA18();
            if (StringUtils.hasText(a18)) {
                String[] split = a18.split(",");
                StringBuilder stringBuilder = new StringBuilder();
                for (int i1 = 0; i1 < split.length; i1++) {
                    String url = split[i1];
                    String sufferName = commonA1.getA10() + "_" + commonA1.getA4() + "_" + (i1 + 1) + ".png";
                    //ExcelUtil.downloadPicture(url, "/Users/meihongliang/Downloads/A_xiaowan/2022-04/week_4/tue/儿童常用OTC/" + sufferName);
                    stringBuilder.append(sufferName).append(";");
                }
                commonA1.setA23(stringBuilder.toString());
            }
        }
        ExcelUtil.writeExcel(commonA, CommonA.class, "/Users/meihongliang/Downloads/A_xiaowan/download/带图片_20220426_0_女性常用OTC匹配上de数据库中数据.xlsx");


        for (int i = 0; i < commonB.size(); i++) {
            CommonA commonA1 = commonB.get(i);
            String a18 = commonA1.getA18();
            if (StringUtils.hasText(a18)) {
                String[] split = a18.split(",");
                StringBuilder stringBuilder = new StringBuilder();
                for (int i1 = 0; i1 < split.length; i1++) {
                    String url = split[i1];
                    String sufferName = commonA1.getA10() + "_" + commonA1.getA4() + "_" + (i1 + 1) + ".png";
                    //ExcelUtil.downloadPicture(url, "/Users/meihongliang/Downloads/A_xiaowan/2022-04/week_4/tue/儿童常用OTC/" + sufferName);
                    stringBuilder.append(sufferName).append(";");
                }
                commonA1.setA23(stringBuilder.toString());
            }
        }
        ExcelUtil.writeExcel(commonB, CommonA.class, "/Users/meihongliang/Downloads/A_xiaowan/download/带图片_20220426_1_肛肠常用OTC匹配上de数据库中数据.xlsx");


        for (int i = 0; i < commonC.size(); i++) {
            CommonA commonA1 = commonC.get(i);
            String a18 = commonA1.getA18();
            if (StringUtils.hasText(a18)) {
                String[] split = a18.split(",");
                StringBuilder stringBuilder = new StringBuilder();
                for (int i1 = 0; i1 < split.length; i1++) {
                    String url = split[i1];
                    String sufferName = commonA1.getA10() + "_" + commonA1.getA4() + "_" + (i1 + 1) + ".png";
                    //ExcelUtil.downloadPicture(url, "/Users/meihongliang/Downloads/A_xiaowan/2022-04/week_4/tue/儿童常用OTC/" + sufferName);
                    stringBuilder.append(sufferName).append(";");
                }
                commonA1.setA23(stringBuilder.toString());
            }
        }
        ExcelUtil.writeExcel(commonC, CommonA.class, "/Users/meihongliang/Downloads/A_xiaowan/download/带图片_20220426_2_儿童常用OTC匹配上de数据库中数据.xlsx");


        for (int i = 0; i < commonD.size(); i++) {
            CommonA commonA1 = commonD.get(i);
            String a18 = commonA1.getA18();
            if (StringUtils.hasText(a18)) {
                String[] split = a18.split(",");
                StringBuilder stringBuilder = new StringBuilder();
                for (int i1 = 0; i1 < split.length; i1++) {
                    String url = split[i1];
                    String sufferName = commonA1.getA10() + "_" + commonA1.getA4() + "_" + (i1 + 1) + ".png";
                    //ExcelUtil.downloadPicture(url, "/Users/meihongliang/Downloads/A_xiaowan/2022-04/week_4/tue/女性常用其他/" + sufferName);
                    stringBuilder.append(sufferName).append(";");
                }
                commonA1.setA23(stringBuilder.toString());
            }
        }
        ExcelUtil.writeExcel(commonD, CommonA.class, "/Users/meihongliang/Downloads/A_xiaowan/download/带图片_20220426_3_女性常用其他匹配上de数据库中数据.xlsx");


    }

    private static void getResult() throws IOException {
        List<CommonAll> allDBData = ExcelUtil.getAllDBData();
        System.out.println(allDBData.size());
        allDBData.forEach(e -> {
            e.setKey2(e.getA5() + "_" + e.getA6());
        });


        List<CommonABCDEFG> commonA = ExcelUtil.readExcel("/Users/meihongliang/EWork/xiaowan/doc/xlsx/2022-04-26需要操作的表.xlsx", CommonABCDEFG.class, 0);
        List<CommonABCDEFG> commonB = ExcelUtil.readExcel("/Users/meihongliang/EWork/xiaowan/doc/xlsx/2022-04-26需要操作的表.xlsx", CommonABCDEFG.class, 1);
        List<CommonABCDEFG> commonC = ExcelUtil.readExcel("/Users/meihongliang/EWork/xiaowan/doc/xlsx/2022-04-26需要操作的表.xlsx", CommonABCDEFG.class, 2);
        List<CommonABCDEFG> commonD = ExcelUtil.readExcel("/Users/meihongliang/EWork/xiaowan/doc/xlsx/2022-04-26需要操作的表.xlsx", CommonABCDEFG.class, 3);

        System.out.println("0_女性常用OTC_" + commonA.size());
        System.out.println("1_肛肠常用OTC_" + commonB.size());
        System.out.println("2_儿童常用OTC_" + commonC.size());
        System.out.println("3_女性常用其他_" + commonD.size());

        commonA.forEach(e -> {
            e.setKey2(e.getA10() + "_" + e.getA2());
        });
        commonB.forEach(e -> {
            e.setKey2(e.getA10() + "_" + e.getA2());
        });
        commonC.forEach(e -> {
            e.setKey2(e.getA10() + "_" + e.getA2());
        });
        commonD.forEach(e -> {
            e.setKey2(e.getA10() + "_" + e.getA2());
        });

        Map<String, List<CommonABCDEFG>> needMap = new HashMap<>();
        needMap.put("0_女性常用OTC", commonA);
        needMap.put("1_肛肠常用OTC", commonB);
        needMap.put("2_儿童常用OTC", commonC);
        needMap.put("3_女性常用其他", commonD);

        // 剩余的未匹配上的
        HashMap<String, List<CommonABCDEFG>> sheetsMap = new HashMap<>();

        needMap.forEach((k, v) -> {
            AtomicInteger count3 = new AtomicInteger(0);
            List<CommonAll> resultCommonA = new LinkedList<>();
            allDBData.forEach(e -> {
                Iterator<CommonABCDEFG> iterator = v.iterator();
                while (iterator.hasNext()) {
                    CommonABCDEFG m = iterator.next();
                    if (e.getA6().equals(m.getA10())) {
                        count3.incrementAndGet();
//                        e.set(m.getA4());
//                        e.setA26(m.getA5());
//                        e.setA27(m.getA6());
//                        e.setA28(m.getA7());
//                        e.setA29(m.getA8());
//                        e.setA30(m.getA9());
                        resultCommonA.add(e);// 收集匹配上的数据库信息
                        iterator.remove();
                    }
                }
            });
            System.out.println("f_" + resultCommonA.size());
            ExcelUtil.writeExcel(resultCommonA, CommonAll.class, "/Users/meihongliang/Downloads/A_xiaowan/download/20220426_" + k + "匹配上de数据库中数据.xlsx");
            resultCommonA.clear();
            sheetsMap.put(k, v);
        });
        //ExcelUtil.writeSheetsToOneFIle("/Users/meihongliang/Downloads/A_xiaowan/download/20220426未匹配上de数据.xlsx", sheetsMap, CommonABCDEFG.class);
        System.out.println(sheetsMap);
    }
}
